import FeatureLayer from '@arcgis/core/layers/FeatureLayer'
import GraphicsLayer from '@arcgis/core/layers/GraphicsLayer.js'
import SimpleFillSymbol from '@arcgis/core/symbols/SimpleFillSymbol'
import SimpleLineSymbol from '@arcgis/core/symbols/SimpleLineSymbol'
import SimpleMarkerSymbol from '@arcgis/core/symbols/SimpleMarkerSymbol'
import Map from '@arcgis/core/Map'
import gaodeLayer from './GaoDeBaseLayer'

// 定义符号样式 - 科技感城市AI风格
export const polygonSymbol = new SimpleFillSymbol({
  color: [0, 120, 255, 0.3], // 科技蓝半透明
  outline: {
    color: [0, 195, 255, 0.9], // 霓虹蓝
    width: 2,
    style: 'solid'
  }
})

export const lineSymbol = new SimpleLineSymbol({
  color: [123, 47, 247, 0.9], // 霓虹紫
  width: 3,
  style: 'solid',
  cap: 'round',
  join: 'round'
})

export const pointSymbol = new SimpleMarkerSymbol({
  color: [0, 255, 143, 0.8], // 霓虹绿
  size: 12,
  outline: {
    color: [255, 255, 255, 0.8],
    width: 1.5
  }
})
// 初始化图层
export const lineLayer = new FeatureLayer({
  url: 'https://services8.arcgis.com/CmXfu09loK81if9H/arcgis/rest/services/DeepseekDesignCityFeaturelayer/FeatureServer/1',
  outFields: ['*'],
  renderer: {
    type: 'simple',
    symbol: lineSymbol,
  } as never,
  popupTemplate: {
    title: '线要素信息',
    content: [
      {
        type: 'fields',
        fieldInfos: [
          { fieldName: 'OBJECTID', label: 'ID' },
          { fieldName: 'RoadName', label: '道路名称' },
          { fieldName: 'RoadType', label: '道路类型' },
          { fieldName: 'description', label: '描述' },
          { fieldName: 'createdAtTime', label: '创建时间' },
        ],
      },
    ],
  },
})

export const polygonLayer = new FeatureLayer({
  url: 'https://services8.arcgis.com/CmXfu09loK81if9H/arcgis/rest/services/DeepseekDesignCityFeaturelayer/FeatureServer/2',
  outFields: ['*'],
  renderer: {
    type: 'simple',
    symbol: polygonSymbol,
  } as never,
  popupTemplate: {
    title: '面要素信息',
    content: [
      {
        type: 'fields',
        fieldInfos: [
          { fieldName: 'OBJECTID', label: 'ID' },
          { fieldName: 'AreaName', label: '区域名称' },
          { fieldName: 'ServiceType', label: '服务类型' },
          { fieldName: 'description', label: '描述' },
          { fieldName: 'createdAtTime', label: '创建时间' },
        ],
      },
    ],
  },
})

export const pointLayer = new FeatureLayer({
  url: 'https://services8.arcgis.com/CmXfu09loK81if9H/arcgis/rest/services/DeepseekDesignCityFeaturelayer/FeatureServer/0',
  outFields: ['*'],
  renderer: {
    type: 'simple',
    symbol: pointSymbol,
  } as never,
  popupTemplate: {
    title: '点要素信息',
    content: [
      {
        type: 'fields',
        fieldInfos: [
          { fieldName: 'OBJECTID', label: 'ID' },
          { fieldName: 'BuildingName', label: '建筑名称' },
          { fieldName: 'BuildingType', label: '建筑类型' },
          { fieldName: 'description', label: '描述' },
          { fieldName: 'createdAtTime', label: '创建时间' },
        ],
      },
    ],
  },
})
// 创建显示图层
export const showLayer = new GraphicsLayer()

// 初始化地图
export const initMap = () => {
  const map = new Map({
    basemap: {
      baseLayers: [gaodeLayer],
    },
  })
  map.addMany([polygonLayer, lineLayer, pointLayer, showLayer])
  return map
}

// 获取目标图层
export const getTargetLayer = (layerType: string): FeatureLayer => {
  switch (layerType) {
    case 'POLYGON':
      return polygonLayer
    case 'LINE':
      return lineLayer
    case 'POINT':
      return pointLayer
    default:
      throw new Error('未知图层类型')
  }
}
